package com.ncmmall.api.apis.mall;

import com.ncmmall.api.apis.service.Result;
import com.ncmmall.api.security.Securitys;
import com.ncmmall.application.business.withdraw.MerchantWithdrawApplication;
import com.ncmmall.domain.business.order.dto.OrderDTO;
import com.ncmmall.domain.business.trading.MerchantWithdrawApply;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author linxs
 * @Description: 提现
 * @Date: 2020-05-11 22:19
 */
@RestController
@RequestMapping(value = "/withdraw")
@Component
public class WithdrawApi {

    private final static Logger logger = LoggerFactory.getLogger(WithdrawApi.class);

    @Autowired
    private MerchantWithdrawApplication merchantWithdrawApplication;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 申请提现
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    @ResponseBody
    public Result submit(HttpServletRequest request, @RequestBody OrderDTO params) {
        Result result = new Result();
        try {

            MerchantWithdrawApply apply = new MerchantWithdrawApply();
            apply.setAmount(params.getAmount());
            apply.setMerchant(Securitys.user(request));
            merchantWithdrawApplication.applyWithdraw(apply);

            return result.ok();

        } catch (Exception e) {
            logger.error("[提交订单异常]", e);
            return result.fail().setMsg("订单提交失败");
        }
    }

    /**
     * 提现列表
     *
     * @param request
     * @param pageNo
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Result list(HttpServletRequest request, @RequestParam(defaultValue = "1") int pageNo, @RequestParam(defaultValue = "20") int pageSize, @RequestParam(defaultValue = "0") int status) {
        Result result = new Result();
        try {
            WebUser member = Securitys.user(request);

            QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageNo).setPageSize(pageSize);
            Map<String, Object> params = Maps.newHashMap();
            params.put("id", member.getId());

            String hql = builderHql("select o from MerchantWithdrawApply o where o.merchant.id = :id ", params, status);
            QueryPage<MerchantWithdrawApply> page = queryChannelService.findPage(hql, pageRequest.setParams(params), MerchantWithdrawApply.class);

            return result.ok().inject(ImmutableMap.of("page", page));

        } catch (Exception e) {
            logger.error("[订单列表异常]", e);
            return result.fail().setMsg("网络请求异常");
        }
    }

    private String builderHql(String sql, Map<String, Object> params, int status) {
        if (status > 0) {
            if (status == 1) {
                sql += " and o.status in (1,2,4)  ";
            } else {
                sql += " and o.status = :status ";
                params.put("status", status);
            }

        }

        sql += " order by o.applyTime desc ";

        return sql;
    }


}
